--[[shader测试

]]
local M = {}

local imgPath1 = "assets/unpack/test/bg1.jpg"
local imgPath2 = "assets/unpack/test/test.png"
local imgPath3 = "assets/unpack/test/tree.png"

local function vertexKernel1()
	-- 顶点着色器
	local vertexKernel = {
		category = "filter",
		name = "flagSharking",
		isTimeDependent = true,
	}
	-- 简单的旗子效果
	vertexKernel.vertex = 
	[[
	P_POSITION vec2 VertexKernel( P_POSITION vec2 position )
	{	
		P_POSITION float factor = 15.0;

		position.y += sin(CoronaTotalTime*3.0 + CoronaTexCoord.x)*factor*CoronaTexCoord.y;

		return position;
	}
	]]
	graphics.defineEffect( vertexKernel )

	local function test()
		local img = display.newImage(imgPath2)
		img.fill.effect = "filter.custom.flagSharking"
		img.x, img.y = SCREEN_CX, SCREEN_CY

		local rect = display.newRect(img.x + img.width/2, img.y + img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x + img.width/2, img.y - img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x - img.width/2, img.y + img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x - img.width/2, img.y - img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)
	end

	test()
end


local function test1()
	local fragmentKernel = {
		category = "filter",
		name = "test1",
	}

	fragmentKernel.fragment = 
	[[
	P_COLOR vec4 FragmentKernel( P_UV vec2 texCoord )
	{
	    P_COLOR float brightness = 0.5;
	    P_COLOR vec4 texColor = texture2D( CoronaSampler0, texCoord );
	 
	    brightness = brightness * texColor.a;
	 
	    // Add the brightness
	    texColor.rgb = brightness;
	 
	    // Modulate by the display object's combined alpha/tint.
	    return CoronaColorScale( texColor );
	}
	]]
	graphics.defineEffect( fragmentKernel )

	local function test()
		local img = display.newImage(imgPath1)
		img.fill.effect = "filter.custom.test1"
		img.x, img.y = SCREEN_CX, SCREEN_CY

		local rect = display.newRect(img.x + img.width/2, img.y + img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x + img.width/2, img.y - img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x - img.width/2, img.y + img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x - img.width/2, img.y - img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)
	end

	test()
end

local function varyingTest()
	local kernel = {}
	kernel.category = "filter"
	kernel.name = "wobbleAndBrighten"
	 
	-- Shader code uses time environment variable CoronaTotalTime
	kernel.isTimeDependent = true
	 
	kernel.vertex =
	[[
	varying P_COLOR float delta; // Custom varying variable
	 
	P_POSITION vec2 VertexKernel( P_POSITION vec2 position )
	{
	    P_POSITION float amplitude = 10.0;
	 
	    position.y += sin( 3.0 * CoronaTotalTime + CoronaTexCoord.x ) * amplitude * CoronaTexCoord.y;
	 
	    // Calculate value for varying
	    delta = 0.4*(CoronaTexCoord.y + sin( 3.0 * CoronaTotalTime + 2.0 * CoronaTexCoord.x ));
	 
	    return position;
	}
	]]

	kernel.fragment =
	[[
	varying P_COLOR float delta; // Matches declaration in vertex shader
	 
	P_COLOR vec4 FragmentKernel( P_UV vec2 texCoord )
	{
	    // Brightness changes based on interpolated value of custom varying variable
	    P_COLOR float brightness = delta;
	 
	    P_COLOR vec4 texColor = texture2D( CoronaSampler0, texCoord );
	 
	    // Pre-multiply the alpha to brightness
	    brightness *= texColor.a;
	 
	    // Add the brightness
	    texColor.rgb += brightness;
	 
	    // Modulate by the display object's combined alpha/tint.
	    return CoronaColorScale( texColor );
	}
	]]
	graphics.defineEffect(kernel)

	local function test()
		local img = display.newImage(imgPath1)
		img.fill.effect = "filter.custom.wobbleAndBrighten"
		img.x, img.y = SCREEN_CX, SCREEN_CY

		local rect = display.newRect(img.x + img.width/2, img.y + img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x + img.width/2, img.y - img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x - img.width/2, img.y + img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)

		local rect = display.newRect(img.x - img.width/2, img.y - img.height/2, 10, 10)
		rect:setFillColor(0, 1, 1)
	end

	test()
end

return M